/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Deal.SalesView.Logic;

import Deal.SalesView.TempData.Carts;
import Deal.SalesView.TempData.Coupon;
import Deal.SalesView.TempData.DisCoupon;
import Deal.SalesView.TempData.EqCoupon;
import Deal.SalesView.TempData.Point;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author 伯乐
 */
public class SalesLogic {
    private Carts carts = new Carts();
    private Coupon coupon = new Coupon("不使用");
    private Point point = new Point();
    //结算
    public double getTotalAccount() {
        String type = coupon.getType();
        switch(type){
            case "打折券":
                return useDiscoupon();
            case "等价券":
                return useEqCoupon();
            default :
                return noCoupon();
        }
    }
    
    public void setType(String type){
        if(!coupon.getType().equals(type)){
            if(type.equals("等价券")){
                coupon = new EqCoupon("等价券");
            } else if(type.equals("打折券")){
                coupon = new DisCoupon("打折券");
            } else {
                coupon = new Coupon("不使用");
            }
        }
    }
    
    public String getType(){
        return coupon.getType();
    }
    
    private double useDiscoupon(){
        double account = noCoupon();
        List<String> valueList = coupon.getCouponList();
        if(valueList.size()>0){
            double value = Double.parseDouble(valueList.get(0));
            return account * value;
        }
        return account;
//        for(int i=0; i<valueList.size(); i++){
//            double value = Double.parseDouble(valueList.get(i));
//            int num = numList.get(i);
//            if(num>0){
//                for(int j=1; j<num; j++){
//                    value *= value;
//                }
//                account *= value;
//            } else {
//                continue;
//            }
//        }
//        return account;
    }
    
    private double useEqCoupon(){
        double account = noCoupon();
        List<String> valueList = coupon.getCouponList();
        List<Integer> numList = coupon.getNumList();
        for(int i=0; i<valueList.size(); i++){
            int value = Integer.parseInt(valueList.get(i));
            account -= value*numList.get(i);
        }
        return account;
    }
    
    private double noCoupon(){
        List<Integer> numList = carts.getNumList();
        List<Double> priceList = carts.getPriceList();
        double account = 0;
        for(int i=0; i<numList.size(); i++){
            account += (numList.get(i)*priceList.get(i));
        }
        return account;
    }
    
    public void cleanTempData(){
        carts.cleanCarts();
        useDiscoupon();
    }
    
    public Vector<List> cartsTableVector() {
        return carts.cartsTableVector();
    }

    public boolean addToCarts(Object[] book, int i) {
        return carts.addBook(book, i);
    }

    public boolean removeBookFromCarts(Object isbn, int num) {
        return carts.remove(isbn, num);
    }
    
    public List<Object[]> getBookInCarts() {
        return carts.getBooklist();
    }
    
    public boolean cartIsEmpty() {
        return carts.cartsIsEnpty();
    }

    public int getUsedPoint() {
        return point.getPoint();
    }

    public void setUsedPoint(int p) {
        point.setPoint(p);
    }
    
    public List<String> getUsedCouponList() {
        return coupon.getCouponList();
    }

    public List<Integer> getCouponNumList() {
        return coupon.getNumList();
    }

    public int getCouponNum(String id) {
        return coupon.getNum(id);
    }

    public Vector getUsedCoupon() {
        return coupon.getCoupon();
    }

    public void refreshData(String id, String value, int num) {
        coupon.refreshData(id, value, num);
    }

}
